{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting gradient descent at b = 0, m = 0, error = 5565.107834483211\n",
      "Running...\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-39-e771fb05359e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m    114\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    115\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'__main__'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 116\u001b[0;31m     \u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    117\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    118\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-39-e771fb05359e>\u001b[0m in \u001b[0;36mrun\u001b[0;34m()\u001b[0m\n\u001b[1;32m    107\u001b[0m     \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"Starting gradient descent at b = {0}, m = {1}, error = {2}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minitial_b\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitial_m\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcompute_error_for_line_given_points\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minitial_b\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitial_m\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpoints\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    108\u001b[0m     \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"Running...\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 109\u001b[0;31m     \u001b[0;34m[\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgradient_descent_runner\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mpoints\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitial_b\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitial_m\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlearning_rate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnum_iterations\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    110\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    111\u001b[0m     \u001b[0;31m# plt.plot(data.x,data.x*m+b)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-39-e771fb05359e>\u001b[0m in \u001b[0;36mgradient_descent_runner\u001b[0;34m(data, points, starting_b, starting_m, learning_rate, num_iterations)\u001b[0m\n\u001b[1;32m     85\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     86\u001b[0m         \u001b[0;31m# plt.draw()\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 87\u001b[0;31m         \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0.5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     88\u001b[0m         \u001b[0;31m# print (\"\\rAfter {0} iterations b = {1}, m = {2}, error = {3}\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     89\u001b[0m         \u001b[0;31m#        .format(num_iterations, b, m, compute_error_for_line_given_points(b, m, points)),end='',flush=True)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAHg9JREFUeJzt3X10HfV95/H3V/ITNjbGsg3GRhJuXMCUhILKw4ayNCQBAiU53SRLqlJ3Q44b6HbT7eY0JmZbso03D7unCU0LOd6QxqldyENLoC0hdZxy2rQN1AYKxjaxm1jgIGxTHmzjYmz5u3/MXCTL9+rO3DvP9/M65x7pju5c/TSSPvOb7/zmN+buiIhIdX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hy5YB9+XTwuy4+y3uvsjd+wkOSb/n7oN04LYwsxlmNrP2OfBOYDNwP8E2gA7ZFgDu/jzwrJmdGS66AthCh26P0AcYLdtAhttCF0zFZGZvJjhx0k2wo/y6u/8vM1tM0KudAzwG/Iq7H8qvpdkys8uBj7r7tZ24LcKf+d7w6STgz9x9lZn1AF8HeoFngPe5+4s5NTNTZnYewUn6KcCPgP9C+D9Dh20PM5sOPAssdvdXwmWZ/W0o6EVEKk6lGxGRilPQi4hUnIJeRKTiFPQiIhWnoBcRqTgFvYhIxSnoRUQqTkEvIlJx/x+ySQ2q+rKuvAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#x = [100,80,120,75,60,43,140,132,63,55,74,44,88]\n",
    "#y = [120,92,143,87,60,50,167,147,80,60,90,57,99]\n",
    "from numpy import *\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import time\n",
    "from IPython import display\n",
    "import threading\n",
    "\n",
    "# %matplotlib inline\n",
    "\n",
    "# y = mx + b\n",
    "# m is slope, b is y-intercept\n",
    "#求误差函数，就是预测值和实际值相减然后求平方，最后再取平均值\n",
    "def compute_error_for_line_given_points(b, m, points):\n",
    "    totalError = 0\n",
    "    #将数据分别赋值为x,y\n",
    "    for i in range(0, len(points)):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        totalError += (y - (m * x + b)) ** 2\n",
    "    return totalError / float(len(points))\n",
    "\n",
    "#梯度下降算法实现，迭代一次后输出一组参数值\n",
    "def step_gradient(b_current, m_current, points, learningRate):\n",
    "    b_gradient = 0\n",
    "    m_gradient = 0\n",
    "    N = float(len(points))\n",
    "    # for i in range(0, len(points)):\n",
    "    #     x = points[i, 0]\n",
    "    #     y = points[i, 1]\n",
    "    #     b_gradient += -(2/N) * (y - ((m_current * x) + b_current))\n",
    "    #     m_gradient += -(2/N) * x * (y - ((m_current * x) + b_current))\n",
    "    for i in range(0, len(points)):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        b_gradient += - (y - ((m_current * x) + b_current))\n",
    "        m_gradient += - x * (y - ((m_current * x) + b_current))\n",
    "        \n",
    "    \n",
    "    new_b = b_current - (learningRate * b_gradient / N)\n",
    "    new_m = m_current - (learningRate * m_gradient / N)\n",
    "    \n",
    "    return [new_b, new_m]\n",
    "\n",
    "\n",
    "gM=0\n",
    "gB=0\n",
    "gData=[]\n",
    "gPoint=[]\n",
    "\n",
    "def refreshGraphic():\n",
    "    while 1:\n",
    "        # print (\"\\rAfter {0} iterations b = {1}, m = {2}, error = {3}\"\n",
    "        #        .format(1000, gB, gM, compute_error_for_line_given_points(gB, gM, gPoint)),end='',flush=True)\n",
    "        \n",
    "        time.sleep(0.5)\n",
    "        # plt.clf()\n",
    "        plt.plot(gData.x,gData.y,\"bo\")\n",
    "        plt.plot(gData.x,gData.x*gM+gB)\n",
    "        plt.show()\n",
    "        display.clear_output(wait=True)\n",
    "        # display.display(plt.gcf())\n",
    "        plt.pause(0.01)\n",
    "\n",
    "        \n",
    "\n",
    "def gradient_descent_runner(data,points, starting_b, starting_m, learning_rate, num_iterations):\n",
    "    global gM\n",
    "    global gB\n",
    "    global gData\n",
    "    gData = data\n",
    "    b = starting_b\n",
    "    m = starting_m\n",
    "    t = threading.Thread(target=refreshGraphic)\n",
    "    t.start()\n",
    "    for i in range(num_iterations):\n",
    "        b, m = step_gradient(b, m, array(points), learning_rate)\n",
    "        gB = b\n",
    "        gM = m\n",
    "        # plt.clf()\n",
    "        # plt.plot(data.x,data.y,\"bo\")\n",
    "        # plt.plot(data.x,data.x*m+b)\n",
    "        # plt.draw()\n",
    "\n",
    "        # plt.draw()\n",
    "        time.sleep(0.5);\n",
    "        # print (\"\\rAfter {0} iterations b = {1}, m = {2}, error = {3}\"\n",
    "        #        .format(num_iterations, b, m, compute_error_for_line_given_points(b, m, points)),end='',flush=True)\n",
    "       \n",
    "        # plt.show()\n",
    "    t.join()\n",
    "    return [b, m]\n",
    "\n",
    "\n",
    "def run():\n",
    "    global gPoint\n",
    "    points = genfromtxt('/Users/xunxun/Downloads/GradientDescentExample-master/data.csv', delimiter=\",\")\n",
    "    gPoint = points\n",
    "    data = pd.read_csv('/Users/xunxun/Downloads/GradientDescentExample-master/data.csv',names=['x','y'])\n",
    "    learning_rate = 0.0001\n",
    "    initial_b = 0 # initial y-intercept guess\n",
    "    initial_m = 0 # initial slope guess\n",
    "    num_iterations = 1000\n",
    "    # plt.plot(data.x,data.y,'bo')\n",
    "\n",
    "    print (\"Starting gradient descent at b = {0}, m = {1}, error = {2}\".format(initial_b, initial_m, compute_error_for_line_given_points(initial_b, initial_m, points)))\n",
    "    print (\"Running...\")\n",
    "    [b, m] = gradient_descent_runner(data,points, initial_b, initial_m, learning_rate, num_iterations)\n",
    "\n",
    "    # plt.plot(data.x,data.x*m+b)\n",
    "    # plt.show()\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    run()\n",
    "\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
